﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DLL.UploadFile
{
	/// <summary>
	/// 上传文件控制类
	/// </summary>
	/// <![CDATA[2014-8-16 11:21:34]]>
	public class Component : DLL.ComponentBase
	{
		#region 属性
		/// <summary>
		/// 问题数据类对象
		/// </summary>
		/// <![CDATA[2014-8-16 11:21:34]]>
		private Database clsDatabase;		// 数据类对象
		/// <summary>
		/// 配置类对象
		/// </summary>
		/// <![CDATA[2014-8-16 11:21:34]]>
		private Config clsConfig = new Config();		// 配置类对象
		/// <summary>
		/// 文件内容控制类
		/// </summary>
		/// <![CDATA[2014-8-17 14:48:15]]>
		private FileContent.Component clsFileContentComp;		// 文件内容控制类
		/// <summary>
		/// 前台栏目控制类
		/// </summary>
		/// <![CDATA[2014-8-20 17:51:30]]>
		private WebColumn.Component clsWebColumnComp;		// 前台栏目控制类
		#endregion 属性

		#region 构造函数
		/// <summary>
		/// 控制类构造函数
		/// </summary>
		/// <![CDATA[2014-8-16 11:21:34]]>
		public Component(System.Web.UI.Page page)
			: base(page)
		{
			clsDatabase = new Database(page);
			clsFileContentComp = new FileContent.Component(page);
			clsData = clsDatabase;
			clsWebColumnComp = new WebColumn.Component(page);

			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nID", clsDatabase.ID, clsDatabase.listStructure[clsDatabase.ID].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nStatus", clsDatabase.STATUS, clsDatabase.listStructure[clsDatabase.STATUS].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strFileName", clsDatabase.FILENAME, clsDatabase.listStructure[clsDatabase.FILENAME].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nWebColumnID", clsDatabase.WEBCOLUMNID, clsDatabase.listStructure[clsDatabase.WEBCOLUMNID].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strDate", clsDatabase.SHOWDATE, clsDatabase.listStructure[clsDatabase.SHOWDATE].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nUserID", clsDatabase.USERID, clsDatabase.listStructure[clsDatabase.USERID].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nDepartmentID", clsDatabase.DEPARTMENTID, clsDatabase.listStructure[clsDatabase.DEPARTMENTID].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strDateTime", clsDatabase.DATETIME, clsDatabase.listStructure[clsDatabase.DATETIME].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strIP", clsDatabase.IP, clsDatabase.listStructure[clsDatabase.IP].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nFileContentID", clsDatabase.FILECONTENTID, clsDatabase.listStructure[clsDatabase.FILECONTENTID].strName));

			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strWebColumn", 0, clsDatabase.clsWebColumnDB.listStructure[clsDatabase.clsWebColumnDB.COLUMNNAME].strName, false));

			listQueryData.Add(new TonyNet4V1.Structs.QueryData("int", "nID", clsDatabase.listStructure[clsDatabase.ID].strName, "="));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("int", "nStatus", clsDatabase.listStructure[clsDatabase.STATUS].strName, "="));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("string", "strWebColumnIDs", clsDatabase.listStructure[clsDatabase.WEBCOLUMNID].strName, "IN"));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("int", "nWebColumnID", clsDatabase.listStructure[clsDatabase.WEBCOLUMNID].strName, "="));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("int", "nDepartmentID", clsDatabase.listStructure[clsDatabase.DEPARTMENTID].strName, "="));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("string", "strDepartmentIDs", clsDatabase.listStructure[clsDatabase.DEPARTMENTID].strName, "IN"));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("int", "nUserID", clsDatabase.listStructure[clsDatabase.USERID].strName, "="));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("string", "strFileName", clsDatabase.listStructure[clsDatabase.FILENAME].strName, "LIKE"));
		}
		#endregion 构造函数

		#region 类型转换
		protected override TonyNet4V1.EntityBase GetEntity()
		{
			return new Entity(page);
		}
		protected override TonyNet4V1.QueryEntityBase GetQuery()
		{
			return new QueryEntity();
		}
		protected override TonyNet4V1.DataBase GetData()
		{
			return new Database(page);
		}
		/// <summary>
		/// 把查询结果链表中的实体类型转为现在的实体类型
		/// </summary>
		/// <![CDATA[2014-8-16 11:21:34]]>
		/// <param name="list"></param>
		/// <returns></returns>
		private List<Entity> ConvertListEntityType(List<TonyNet4V1.EntityBase> list)
		{
			List<Entity> listEntity = new List<Entity>();

			for (int i = 0; i < list.Count; i++)
			{
				Entity clsEntity = new Entity();

				clsEntity = (Entity)list[i];

				listEntity.Add(clsEntity);
			}

			return listEntity;
		}
		#endregion 类型转换

		#region 查询
		/// <summary>
		/// 根据ID获得实体对象
		/// </summary>
		/// <![CDATA[2014-8-16 11:21:34]]>
		/// <param name="nID"></param>
		/// <returns></returns>
		public new Entity GetOne(int nID)
		{
			return (Entity)base.GetOne(nID);
		}

		/// <summary>
		/// 查询所有
		/// </summary>
		/// <![CDATA[2014-8-16 11:21:34]]>
		/// <returns></returns>
		public new List<Entity> QueryAll()
		{
			return ConvertListEntityType(base.QueryAll());
		}

		/// <summary>
		/// 查询
		/// </summary>
		/// <![CDATA[2014-8-16 11:21:34]]>
		/// <returns></returns>
		public List<Entity> Query(QueryEntity clsQuery)
		{
			return ConvertListEntityType(base.Query(clsQuery));
		}
		#endregion 查询
		
		#region 添加、修改等
		/// <summary>
		/// 添加
		/// </summary>
		/// <![CDATA[2014-8-17 10:41:43]]>
		/// <param name="clsEntity"></param>
		/// <returns></returns>
		public override bool Add(TonyNet4V1.EntityBase clsEntity)
		{
			Entity clsAddUploadFile = new Entity();

			clsAddUploadFile = (Entity)clsEntity;
			clsAddUploadFile.DateTime = System.DateTime.Now.ToString();
			//clsAddUploadFile.IP = ;
			
			return base.Add(clsAddUploadFile);
		}
		/// <summary>
		/// 添加
		/// </summary>
		/// <![CDATA[2014-8-17 14:47:24]]>
		/// <param name="clsEntity"></param>
		/// <param name="fUpload"></param>
		/// <returns></returns>
		public bool Add(Entity clsEntity, System.Web.UI.WebControls.FileUpload fUpload)
		{
			FileContent.Entity clsFileContent = new FileContent.Entity();

			clsFileContent = clsFileContentComp.Add(fUpload);
			if (clsFileContent != null)
			{
				clsEntity.FileContentID = clsFileContent.ID;
				clsEntity.FileName = clsFileContent.FileName;
			}
			else
			{
				
			}

			return Add(clsEntity);
		}
		#endregion 添加、修改等

		protected override TonyNet4V1.EntityBase DataToEntity(System.Data.DataRow dr)
		{
			Entity clsUploadFile = new Entity();
			Department.Component clsDepartmentComp = new Department.Component(page);
			Department.Entity clsDepartment = new Department.Entity();

			clsUploadFile = (Entity) base.DataToEntity(dr);
			clsDepartment = clsDepartmentComp.GetOne(clsUploadFile.DepartmentID);
			if (clsDepartment != null)
			{
				clsUploadFile.DepartmentName = clsDepartment.DepartmentName;
			}

			return clsUploadFile;
		}

		/// <summary>
		/// 根据前台栏目ID查询本栏目和其子栏目的文件
		/// </summary>
		/// <![CDATA[2014-8-20 17:39:59]]>
		/// <param name="nWebColumnID">前台栏目ID</param>
		/// <returns></returns>
		public List<Entity> QueryByWebColumnID(int nWebColumnID)
		{
			string strWebColumnIDs;
			QueryEntity clsQuery = new QueryEntity();

			strWebColumnIDs = clsWebColumnComp.QueryColumnAndChildColumn(nWebColumnID);
			clsQuery.strWebColumnIDs = strWebColumnIDs;

			return Query(clsQuery);
		}

		/// <summary>
		/// 根据上传文件所属单位查找上传到基层动态里的文件
		/// </summary>
		/// <param name="nDepartmentID">单位ID字符串</param>
		/// <returns>上传文件链表</returns>
		public List<Entity> QueryByDepartmentIDs(string strDepartmentIDs)
		{
			QueryEntity clsQuery = new QueryEntity();

			clsQuery.strDepartmentIDs = strDepartmentIDs;
			clsQuery.nWebColumnID = 15;
			
			return Query(clsQuery);
		}

		/// <summary>
		/// 根据文件名查找记录
		/// </summary>
		/// <returns></returns>
		public List<Entity> QueryByFileName(string strFileName)
		{
			QueryEntity clsQuery = new QueryEntity();
			WebColumn.Component clsWebColumnComp = new WebColumn.Component(page);
			string strWebColumns = "";
			List<WebColumn.Entity> listWebColumn = new List<WebColumn.Entity>();

			listWebColumn = clsWebColumnComp.QueryAll();
			for (int i = 0; i < listWebColumn.Count; i++)
			{
				strWebColumns = strWebColumns + listWebColumn[i].ID.ToString();
				if (i != listWebColumn.Count - 1)
				{
					strWebColumns = strWebColumns + ", ";
				}
			}

			clsQuery.strFileName = strFileName;
			clsQuery.strWebColumnIDs = strWebColumns;

			return Query(clsQuery);
		}
	}
}
